package com.dailyblue.java.boot.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dailyblue.java.boot.mybatisplus.bean.User;
import com.dailyblue.java.boot.mybatisplus.service.UserService;
import com.dailyblue.java.spring.boot.commons.exception.UserCodeNotFoundException;
import com.dailyblue.java.spring.boot.commons.exception.UserPasswordErrorException;
import com.dailyblue.java.spring.boot.commons.util.JsonResult;
import com.dailyblue.java.spring.boot.commons.util.ResultCode;
import com.dailyblue.java.spring.boot.commons.util.ResultTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @GetMapping("/a")
    public String a(HttpServletResponse response) {
        //response.addHeader("Access-Control-Allow-Origin","*");
        System.out.println("aaaa");
        return "aaaa";
    }

    @Resource
    private UserService userService;

    //@CrossOrigin
    // localhost:8082/user get请求会进入这里
    @GetMapping
    public JsonResult find() {
        return userService.findAll();
    }

    // delete user where id=#{id}
    @DeleteMapping
    public JsonResult delete(int id) {
        return userService.deleteById(id);
    }

    @PostMapping
    public JsonResult save(User user) {
        String password = user.getPassword();
        password = DigestUtils.md5Hex(password);
        user.setPassword(password);
        log.info("获取的用户是：{}", user);
        boolean b = userService.save(user);
        return b ? ResultTool.success(null) : ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    @PutMapping
    public JsonResult update(User user) {
        boolean b = userService.saveOrUpdate(user);
        return b ? ResultTool.success(null) : ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    // 条件查询
    // {phone}是Rest风格传值方式
    @GetMapping("/phone/{phone}")
    public JsonResult checkPhone(@PathVariable("phone") String phone, @RequestParam(defaultValue = "0") int id) {
        // 条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 验证你的手机号是否存在 eq:代表相等
        wrapper.eq("phone", phone);
        if (id != 0) {
            wrapper.ne("id", id);
        }
        // 调用了count操作
        long count = userService.count(wrapper);
        return count == 0 ? ResultTool.success("") : ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    @GetMapping("/code/{userCode}")
    public JsonResult checkUserCode(@PathVariable("userCode") String userCode, @RequestParam(defaultValue = "0") int id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode", userCode);
        if (id != 0) {
            wrapper.ne("id", id);
        }
        long count = userService.count(wrapper);
        return count == 0 ? ResultTool.success("") : ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    @GetMapping("/job")
    public JsonResult findAllJob() {
        return userService.findAllJob();
    }

    @GetMapping("/id/{id}")
    public JsonResult findUserById(@PathVariable Integer id) {
        return userService.findUserById(id);
    }

    @PostMapping("/login")
    public JsonResult login(User user) throws UserCodeNotFoundException, UserPasswordErrorException {
        return userService.login(user);
    }
}
